import configparser as con
import os
base_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(base_dir, "my.ini")
print(file_path)
conf = con.ConfigParser()
# 添加节点
conf.add_section("嫣然")
conf.set("嫣然", "sex", "女")
conf.set("嫣然", "age", "19")
conf.add_section("紫嫣")
conf.set("紫嫣", "sex", "女")
conf.set("紫嫣", "age", "20")
conf.add_section("萌萌")
conf.set("萌萌", "sex", "女")
conf.set("萌萌", "age", "21")
with open(file_path, mode="w", encoding="utf-8") as f:
    conf.write(f)
conf.read("my.ini", encoding="UTF-8")
# 获取所有的节点
j = conf.sections()
print("获取所有的节点:", j)
# 获取节点下的键值
key_value = conf.items("嫣然")
print("获取节点下的键值:", key_value)
# 节点下的某个键对应的值
value = conf.get("嫣然", "sex")
print("节点下的某个键对应的值:", value)
# 是否存在某个节点
is_j = conf.has_section("server")
print("是否存在某个节点:", is_j)
# 删除某个节点 删除完还得保存一下
# conf.remove_section("紫嫣")
# # 删除某个键值
# conf.remove_option("紫嫣", "age")
# with open(file_path, mode="w", encoding="utf-8") as f:
#     conf.write(f)
